package com.ruoyi.article.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.article.vo.AddTagAndCategoryRequest;
import com.ruoyi.article.vo.SubmitRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.article.domain.Article;
import com.ruoyi.article.service.IArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章 Controller
 * 
 * @author rey
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/article/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    /**
     * 查询文章 列表
     */
    @PreAuthorize("@ss.hasPermi('article:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 导出文章 列表
     */
    @PreAuthorize("@ss.hasPermi('article:article:export')")
    @Log(title = "文章 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章 数据");
    }

    /**
     * 获取文章 详细信息
     */
    @PreAuthorize("@ss.hasPermi('article:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return AjaxResult.success(articleService.selectArticleByArticleId(articleId));
    }

    /**
     * 新增文章 
     */
    @PreAuthorize("@ss.hasPermi('article:article:add')")
    @Log(title = "文章 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章 
     */
    @PreAuthorize("@ss.hasPermi('article:article:edit')")
    @Log(title = "文章 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章 
     */
    @PreAuthorize("@ss.hasPermi('article:article:remove')")
    @Log(title = "文章 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(articleService.deleteArticleByArticleIds(articleIds));
    }

    /**
     * 启用文章
     */
    @PreAuthorize("@ss.hasPermi('article:article:submit')")
    @Log(title = "文章 ", businessType = BusinessType.DELETE)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody SubmitRequest submitRequest)
    {
        return toAjax(articleService.submitArticleByArticleIds(submitRequest.getArticleIds(),submitRequest.getEnable()));
    }

    /**
     * 添加分类和标签
     */
    @PreAuthorize("@ss.hasPermi('article:article:submit')")
    @Log(title = "文章 ", businessType = BusinessType.UPDATE)
    @PostMapping("/addTagAndCategory")
    public AjaxResult addTagAndCategory(@RequestBody AddTagAndCategoryRequest request)
    {
        return toAjax(articleService.addTagAndCategory(request));
    }

}
